package com.soushin.mvpdemo.module.main;

import android.content.Intent;
import android.view.View;

import com.blankj.ALog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.soushin.mvpdemo.Utils.PictureUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;

import com.soushin.mvpdemo.R;
import com.soushin.mvpdemo.base.BaseActivity;
import com.soushin.mvpdemo.module.bigimage.BigImageActivity;
import com.soushin.mvpdemo.module.login.LoginActivity;
import com.soushin.mvpdemo.module.rxjava.RxjavaActivity;

/**
 * View
 */
public class MainActivity extends BaseActivity<MainView, MainPresenter> implements MainView {

    private String[] permissions = new String[]{Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA};

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void bindView() {
        getPresenter().subscribe(this);
    }

    @Override
    public void initView() {
        AndPermission.with(getActivity())
                .runtime().permission(permissions)
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        showToasty("权限已被拒绝");
                    }
                }).start();
    }

    public void BigImage(View view) {
        goTo(BigImageActivity.class);
//        方法多种多样 实现全靠想象
//        ActivityUtil.skipActivity(getActivity(),BigImageActivity.class);
    }

    public void RxJavaClick(View view) {
        goTo(RxjavaActivity.class);
    }

    public void mvpClick(View view) {
        goTo(LoginActivity.class);
    }

    public void imageSelect(View view) {
        if (!AndPermission.hasPermissions(getActivity(), Permission.WRITE_EXTERNAL_STORAGE)) {
            showToasty("请到设置中开启权限");
            return;
        }
        PictureUtils.openCamera(getActivity(),true,true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    ALog.e("选择的图片",Thread.currentThread().getName(),selectList.get(0).getCompressPath());
                    PictureFileUtils.deleteCacheDirFile(getActivity());
                    break;
            }
        }
    }



}
